package com.daikuan.lizh.adapter;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.daikuan.lizh.R;
import com.daikuan.lizh.bmobBean.ListConfig;
import com.daikuan.lizh.ui.activity.YouXuanActivity;
import com.daikuan.lizh.utils.ImageLoader;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by jiming1 on 2017/8/2.
 */
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    List<ListConfig> groups;
    List<List<ListConfig>> childs;
    private Activity context;

    public ExpandableListViewAdapter(List<ListConfig> groups, List<List<ListConfig>> childs, Activity context) {
        this.groups = groups;
        this.childs = childs;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return childs.get(i).size();
    }

    @Override
    public ListConfig getGroup(int i) {
        return groups.get(i);
    }

    @Override
    public ListConfig getChild(int i, int i1) {
        return childs.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        view = View.inflate(context, R.layout.item_bar, null);
        TextView textView = (TextView) view.findViewById(R.id.tvName);
        textView.setText(getGroup(i).list_name);
        view.findViewById(R.id.tvMore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ListConfig group = getGroup(i);
                YouXuanActivity.jump(group.list_id, group.list_name, context);
            }
        });
        return view;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        BaseViewHolder viewHolder = null;
        final ListConfig hotChannel = getChild(i, i1);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_youxuan2, viewGroup, false);
            viewHolder = new BaseViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (BaseViewHolder) view.getTag();
        }

        if (hotChannel.channel_logo == null) {
            ImageLoader.loadImage(context, "", viewHolder.ivImage);
        } else {
            ImageLoader.loadImage(context, hotChannel.channel_logo.url, viewHolder.ivImage);
        }
        viewHolder.tvName.setText(hotChannel.channel_name);
        viewHolder.tvQuotaRange.setText(hotChannel.money_range);
//        viewHolder.tvSuccessRate.setText(hotChannel.successRate);
        if (!TextUtils.isEmpty(hotChannel.tags)) {
            String[] tags = hotChannel.tags.split("，");
            if (tags != null && tags.length > 0) {
                if (tags.length == 1) {
                    viewHolder.tvLabel01.setVisibility(View.VISIBLE);
                    viewHolder.tvLabel01.setText(tags[0]);
                } else if (tags.length == 2) {
                    viewHolder.tvLabel02.setVisibility(View.VISIBLE);
                    viewHolder.tvLabel02.setText(tags[1]);
                    viewHolder.tvLabel01.setVisibility(View.VISIBLE);
                    viewHolder.tvLabel01.setText(tags[0]);
                } else if (tags.length == 3) {
                    viewHolder.tvLabel03.setVisibility(View.VISIBLE);
                    viewHolder.tvLabel03.setText(tags[2]);
                    viewHolder.tvLabel02.setVisibility(View.VISIBLE);
                    viewHolder.tvLabel02.setText(tags[1]);
                    viewHolder.tvLabel01.setVisibility(View.VISIBLE);
                    viewHolder.tvLabel01.setText(tags[0]);
                }
            }
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class BaseViewHolder {
        @Bind(R.id.ivImage)
        ImageView ivImage;
        @Bind(R.id.tvName)
        TextView tvName;
        @Bind(R.id.tvQuotaRange)
        TextView tvQuotaRange;
        @Bind(R.id.tvLabel01)
        TextView tvLabel01;
        @Bind(R.id.tvLabel02)
        TextView tvLabel02;
        @Bind(R.id.tvLabel03)
        TextView tvLabel03;


        public BaseViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
